package com.ethink.order.service.impl;

import com.ethink.id.generator.IdGenerator;
import com.ethink.order.api.info.SaleDetailOrderInfo;
import com.ethink.order.api.request.SaleDemandOrderDetailAddRequest;
import com.ethink.order.api.request.SaleDetailOrderAddRequest;
import com.ethink.order.api.utils.EthinkStrUtil;
import com.ethink.order.common.entity.SaleDemandOrderDetail;
import com.ethink.order.common.entity.SaleDetailOrder;
import com.ethink.order.common.mapper.SaleDetailOrderMapper;
import com.ethink.order.converter.SaleDetailOrderConverter;
import com.ethink.order.manager.PublicManager;
import com.ethink.order.service.SaleDetailOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * TOC销售详单 Service接口实现
 *
 * @author default
 * @date 2022-11-22
 */
@Slf4j
@Service
public class SaleDetailOrderServiceImpl implements SaleDetailOrderService {

    @Autowired
    private SaleDetailOrderMapper saleDetailOrderMapper;

    @Autowired
    private SaleDetailOrderConverter saleDetailOrderConverter;

    @Autowired
    private IdGenerator idGenerator;

    @Autowired
    private PublicManager publicManager;


    @Override
    public void batchInsert(List<SaleDetailOrderAddRequest> item, String orderNo, BigDecimal exchangeRate) {
        for (SaleDetailOrderAddRequest SaleDetailOrderAddRequest : item) {
            addSaleDetailOrder(SaleDetailOrderAddRequest, orderNo, exchangeRate);
        }
    }

    @Override
    public Integer addSaleDetailOrder(SaleDetailOrderAddRequest request, String orderNo, BigDecimal exchangeRate) {
        SaleDetailOrder saleDetailOrder = new SaleDetailOrder();
        //生成随机单号
        String detailOrderNo = idGenerator.xsetail();
        BeanUtils.copyProperties(request, saleDetailOrder);
        saleDetailOrder.setDetailOrderNo(detailOrderNo);
        saleDetailOrder.setOrderNo(orderNo);
        String cateName = EthinkStrUtil.stitchStrings(request.getGeneraCodeName(), request.getMinorDefectsCodeName(), request.getSubclassCodeName());
        saleDetailOrder.setCategoryName(cateName);
        saleDetailOrder.setSalePriceBb(saleDetailOrder.getSalePrice().multiply(exchangeRate));
        return saleDetailOrderMapper.insert(saleDetailOrder);

    }

    @Override
    public List<SaleDetailOrderInfo> queryDetailListByOrderNo(String orderNo) {
        return saleDetailOrderMapper.queryDetailListByOrderNo(orderNo);
    }

    @Override
    public List<String> querySaleOrderDetailOrderNoByOrderNo(List<String> orderNos) {
        return saleDetailOrderMapper.querySaleOrderDetailOrderNoByOrderNo(orderNos);
    }
}
